<template>
	<view class="wrap">
		<view class="content" v-if="lists.length > 0">
			<block v-for="(item, inex) in lists" :key="index">
				<view class="item">
					<view class="detail">
						<view class="title">
							<view class="type">{{item.type == 1 ? '补卡' : item.type == 2 ? '请假' : item.type == 3 ? '出差' : item.type == 4 ? '外出' : item.type == 5 ? '加班' : '换班'}}申请</view>
							<view class="status">{{item.status == 1 ? '待审批' : item.status == 3 ? '领导审批拒绝' : item.status == 5 ? '领导审批通过' : item.status == 7 ? '审核员审批拒绝' : item.status == 10 ? '审核员审批通过' : ''}}</view>
						</view>
						<block v-if="item.type != 1">
							<view class="label">
								<view class="text">申请人</view>
								<text>{{item.user.nickname}}</text>
							</view>
							<view class="label">
								<view class="text">开始时间：</view>
								<text>{{item.start_time_text}}</text>
							</view>
							<view class="label">
								<view class="text">结束时间：</view>
								<text>{{item.end_time_text}}</text>
							</view>
						</block>
						<block v-if="item.type == 1">
							<view class="label">
								<view class="text">申请人</view>
								<text>{{item.user.nickname}}</text>
							</view>
							<view class="label">
								<view class="text">补卡时间：</view>
								<text>{{item.start_time_text}}</text>
							</view>
						</block>
						<view class="label" v-if="item.type == 3">
							<view class="text">目的地：</view>
							<text>{{item.to_address}}</text>
						</view>
						<view class="label" v-if="item.type == 2">
							<view class="text">请假类型：</view>
							<text>{{item.type1}}</text>
						</view>
						<view class="label">
							<view class="text">申请理由：</view>
							<text>{{item.remark}}</text>
						</view>
						<view class="label" v-if="item.type == 1">
							<view class="text">图片：</view>
							<image :src="item.image" mode="widthFix"></image>
						</view>
					</view>
					<view class="bottom" v-if="item.status == 1 && is_audit == 0">
						<view class="btn" @click="show = true, currentid = item.id, status = 5">同意</view>
						<view class="btn refuse" @click="show = true, currentid = item.id, status = 3">拒绝</view>
					</view>
					<view class="bottom" v-if="(item.status == 1 || item.status == 5) && is_audit == 1">
						<view class="btn" @click="show = true, currentid = item.id, status = 10">同意</view>
						<view class="btn refuse" @click="show = true, currentid = item.id, status = 7">拒绝</view>
					</view>
				</view>
			</block>
		</view>
		<u-empty mode="list" v-else></u-empty>
		<u-popup :show="show" @close="show = false" mode="center">
			<view class="remark">
				<view class="title">审批说明</view>
				<view class="textarea">
					<u--textarea v-model="remark" placeholder="请输入内容" ></u--textarea>
				</view>
				<view class="bottom" @click="handleAudit">确定</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
import { officesAudit, officesFinalAudit, officesList } from '../../../api/inner'
	
	export default {
		data () {
			return {
				page: 1,
				page_size: 15,
				last_page: 1,
				lists: [],
				show: false,
				remark: '',
				status: '',
				currentid: '',
				userId: '',
				is_audit: 0
			}
		},
		onLoad () {
			this.getData()
			this.is_audit = uni.getStorageSync('userinfo').is_audit
			this.userId = uni.getStorageSync('userid')
		},
		onReachBottom() {
			if(this.page == this.last_page){
				uni.showToast({
					title:'已经是最后一页了',
					icon:'none'
				})
			}else{
				this.page++
				this.getData()
			}
		},
		methods: {
			async getData () {
				if(this.page == 1){
					this.lists = []
				}
				let params = {
					page: this.page,
					pageSize: this.page_size
				}
				let res = await officesList(params)
				
				this.last_page = res.data.last_page
				if (res.code == 1) {
					res.data.data.forEach((item,index) => {
						this.lists.push(item)
					})
				} else {
					uni.showToast({
						title:res.msg,
						icon:'none'
					})
				}
			},
			
			async handleAudit () {
				if (!this.remark) {
					uni.$u.toast('审批说明不能为空')
					return
				}
				let params = {
					id: this.currentid,
					status: this.status,
					audit_remark: this.remark
				}
				if (this.is_audit == 1) {
					var res = await officesFinalAudit(params)
				} else {
					var res = await officesAudit(params)
				}
				this.show = false
				uni.$u.toast(res.msg)
				this.page = 1
				this.getData()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.wrap {
		min-height: 100vh;
		height: fit-content;
		background-color: #F2F2F2;
		box-sizing: border-box;
		
		.content {
			padding: 12px;
			box-sizing: border-box;
			
			.item {
				padding: 12px;
				border-radius: 10px;
				background-color: #fff;
				box-sizing: border-box;
				margin-bottom: 15px;
				
				.title {
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-bottom: 10px;
					
					.type {
						font-size: 20px;
						font-weight: bold;
					}
					
					.status {
						font-size: 16px;
					}
				}
				
				.label {
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-bottom: 12px;
					
					.text {
						display: block;
						width: 30%;
						font-size: 16px;
						color:#666;
					}
					
					text {
						flex: 1;
						display: block;
						font-size: 16px;
						color:#666;
					}
					
					image {
						display: block;
						width: 65vw;
						border-radius: 10px;
					}
				}
				
				.bottom {
					display: flex;
					align-items: center;
					justify-content: flex-end;
					margin-top: 20px;
					
					.btn {
						width: fit-content;
						height: 28px;
						font-size: 24rpx;
						color: #fff;
						padding: 0 36rpx;
						line-height: 28px;
						border-radius: 14px;
						background-color: #0a84ff;
						box-sizing: border-box;
					}
					
					.refuse {
						background-color: #DE1F28;
						margin-left: 24rpx;
					}
				}
			}
		}
		
		.remark {
			width: 80vw;
			padding: 32rpx 24rpx;
			border-radius: 20rpx;
			background-color: #fff;
			box-sizing: border-box;
			display: flex;
			flex-direction: column;
			align-items: center;
			
			.title {
				width: 100%;
				font-size: 30rpx;
				font-weight: bold;
				margin-bottom: 24rpx;
			}
			
			.textarea {
				width: 100%;
				margin-bottom: 48rpx;
			}
			
			.bottom {
				width: fit-content;
				height: 64rpx;
				line-height: 64rpx;
				padding: 0 80rpx;
				color: #fff;
				font-size: 26rpx;
				background-color: #0a84ff;
				border-radius: 32rpx;
				box-sizing: border-box;
			}
		}
	}
</style>